'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.genLocalStorage = exports.genMemoryStorage = exports.genStorage = exports.MemoryStorage = undefined;

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _require = require('../safeJson'),
    safeJson = _require.safeJson;

var _require2 = require('../safeStringify'),
    safeStringify = _require2.safeStringify;

var _require3 = require('../getPromiseValue'),
    getPromiseValue = _require3.getPromiseValue;

var get = require('lodash/get');
var set = require('lodash/set');

var MemoryStorage = exports.MemoryStorage = function () {
    function MemoryStorage() {
        (0, _classCallCheck3.default)(this, MemoryStorage);
        this.data = {};
    }

    (0, _createClass3.default)(MemoryStorage, [{
        key: 'get',
        value: function get(key) {
            return this.data[key];
        }
    }, {
        key: 'set',
        value: function set(key, data) {
            return this.data[key] = data;
        }
    }, {
        key: 'remove',
        value: function remove(key) {
            return delete this.data[key];
        }
    }]);
    return MemoryStorage;
}();

var _localStorage = {
    get: function get(key) {
        return localStorage.getItem(key);
    },
    set: function set(key, data) {
        return localStorage.setItem(key, data);
    },
    remove: function remove(key) {
        return localStorage.removeItem(key);
    }
};

var Storage = function () {
    function Storage() {
        (0, _classCallCheck3.default)(this, Storage);
        this.storage = null;
        this.autoJson = true;
        this.event = null;
        this.namespace = '';
    }

    (0, _createClass3.default)(Storage, [{
        key: 'getKey',
        value: function getKey(key) {
            return this.namespace + '_' + key;
        }
    }, {
        key: 'get',
        value: function get(key) {
            var _this = this;

            var _key = this.getKey(key);
            return getPromiseValue(this.storage.get(_key)).then(function (data) {
                if (!_this.autoJson) {
                    return data;
                }
                return safeJson(data);
            });
        }
    }, {
        key: 'set',
        value: function set(key, data) {
            var _this2 = this;

            var _data = data;
            if (this.autoJson) {
                _data = safeStringify(data);
            }
            var _key = this.getKey(key);
            return getPromiseValue(this.storage.set(_key, _data)).then(function (rst) {
                _this2.event && _this2.event.emit('change', { key: key, data: data });
                return rst;
            });
        }
    }, {
        key: 'remove',
        value: function remove(key) {
            var _this3 = this;

            return getPromiseValue(this.storage.remove(key)).then(function (rst) {
                _this3.event && _this3.event.emit('remove', { key: key });
                return rst;
            });
        }
    }, {
        key: 'getData',
        value: function getData(key, path, defVal) {
            return this.get(key).then(function (data) {
                return get(data, path, defVal);
            });
        }
    }, {
        key: 'setData',
        value: function setData(key, path, val) {
            var _this4 = this;

            return this.get(key).then(function (config) {
                var _config = config || {};
                set(_config, path, val);
                return _this4.set(key, _config);
            });
        }
    }, {
        key: 'setStorage',
        value: function setStorage(storage) {
            this.storage = storage;
        }
    }, {
        key: 'setEvent',
        value: function setEvent(event) {
            this.event = event;
        }
    }, {
        key: 'getNamespace',
        value: function getNamespace() {
            return this.namespace;
        }
    }, {
        key: 'setNamespace',
        value: function setNamespace() {
            var namespace = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';

            this.namespace = namespace;
        }
    }]);
    return Storage;
}();

var genStorage = exports.genStorage = function genStorage() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        storage = _ref.storage,
        _ref$event = _ref.event,
        event = _ref$event === undefined ? null : _ref$event,
        _ref$namespace = _ref.namespace,
        namespace = _ref$namespace === undefined ? '' : _ref$namespace;

    var rst = new Storage();
    rst.setStorage(storage);
    rst.setEvent(event);
    rst.setNamespace(namespace);
    return rst;
};

var genMemoryStorage = exports.genMemoryStorage = function genMemoryStorage(opt) {
    return genStorage((0, _extends3.default)({}, opt, {
        storage: new MemoryStorage()
    }));
};

var genLocalStorage = exports.genLocalStorage = function genLocalStorage(opt) {
    return genStorage((0, _extends3.default)({}, opt, {
        storage: _localStorage
    }));
};
exports.default = genStorage;